package com.divemotor.ventarepuestosinternet.dao.impl;

import java.io.Serializable;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import com.divemotor.ventarepuestosinternet.dao.TipoVehiculoDAO;
import com.divemotor.ventarepuestosinternet.model.TipoVehiculo;


@Repository(value = "tipoVehiculoDAO")
public class TipoVehiculoDAOImpl implements TipoVehiculoDAO, Serializable {

	private static final long serialVersionUID = 1084508996087262280L;
	
	@Autowired
	private SessionFactory sessionFactory;

	public SessionFactory getSessionFactory() {
		return sessionFactory;
	}

	
	@SuppressWarnings("unchecked")
	@Override
	public List<TipoVehiculo> listar() {
		return sessionFactory
				.getCurrentSession()
				.createCriteria(TipoVehiculo.class)
				.addOrder(Order.asc("nombre"))
				.list();
	}
	
	@Override
	public TipoVehiculo obtenerPorCodigo(TipoVehiculo tipoVehiculo) {	
		Criteria criteria = sessionFactory.getCurrentSession().createCriteria(TipoVehiculo.class)
				.add(Restrictions.eq("codigoTipoVehiculo", tipoVehiculo.getCodigoTipoVehiculo()));
		return (TipoVehiculo) criteria.uniqueResult();
	}
	
}
